<?php
/**
 * 
 * 实现功能：
 * 实现了入口的导航逻辑
 * 项目的入口 （也是接收code的入口）
 * http://192.168.33.10/CI/index.php/main_control/entrance
 * http://192.168.33.10/CI/index.php/main_control/daohang
 * 
 * 具体说明：
 * 回调页面硬编码了入口URL（ base_url().'/index.php/main_control/entrance'）
 * 		a)微信回调页面获取code：http://12002shop.web08.com.cn/hfjx/wx/codeback.html
 * 		b)通过回调页面上的提交按钮将code提交到入口URL（ base_url().'/index.php/main_control/entrance'）
 * 		c)获取到openId，根据openId得到user 然后根据结果跳转到注册、审核提醒、首页的view
 * 
 * 关键点说明：
 * 1) debug模式的修改
 *     _myconfig.php : $config['mydebug']
 *     debug模式主要是为了在本地设置openId
 * 2） entrance() 方法	
 * 		该方法对应view（view/jjr/entrance.php） 页面中使用了sessionStorage缓存，主要保存了openId等相关信息
 *      方便在导航跳转时候识别客户
 * 
 *  相关配置参数：
 *  _myconfig.php : $config['json_url_base']   $config['mydebug']
 *  database.php
 *  config.php : $config['base_url']
 * 
 * 
 */

class Main_Control extends CI_Controller {

	
	/**可看作是总入口 对应index视图
	 * 通过code获取openId并得到user的状态
	 * state:-1,0,1 三中状态决定了显示“注册”、“首页”还是“待审核”的区块
	 */
	function entrance(){
		$this->load->helper('url');
		$this->config->load('_myconfig.php');
		
		/* 导航菜单需要用到的 */
		$openId = isset($_POST['openId'])?$_POST['openId']:'';
		$creator = isset($_POST['creator'])?$_POST['creator']:0;
		$state = isset($_POST['state'])?$_POST['state']:'';
		$item = isset($_POST['item'])?$_POST['item']:'';
		$header_data['openId']=$openId;
		$header_data['state']=$state;
		$header_data['item']=$item;
		$header_data['title']=$this->config->item('mytitle');;
		$header_data['showdebug']=$this->config->item('showdebug');
		
		$debug = $this->config->item('mydebug');
		if($debug==false){	
			$code = isset($_POST['code'])?$_POST['code']:'';
			
			if($code==''){
				echo "网络故障请重新登陆";
				die();
			}
			
			$this->load->model('wechat_model');
			$openId = $this->wechat_model->getOpenId($code);	// 由code获取openId
			
			if($openId==''){
				echo "网络故障请重新登陆";
				die();
			}
		
		}else 
			$openId="aaa";
		
		$this->load->model('user_model');
		$user = $this->user_model->getUserByOpenId($openId);	//由openId查询user
		$state = isset($user['state'])?$user['state']:-1;
		$flag = isset($flag)?$flag:-1;
		
		$name = isset($user['name'])?$user['name']:'';
		$uid = isset($user['id'])?$user['id']:$creator;
		
		$data['openId']=$openId;	// 便于entrance.php缓存使用，方便在导航跳转时候识别客户
		$data['uid']=$uid;
		$data['creator']=$uid;
		$data['state']=$state;
		$data['name']=$name;
		$data['flag']=$flag;
		
		if($state==0){
			$this->load->view('jjr/block/header',$header_data);
			$this->load->view('jjr/index',$data);
			$this->load->view('jjr/block/_footer');
		}
		
		if($state==-1 || $state==1){
			$this->load->view('jjr/block/header',$header_data);
			$this->load->view('jjr/index',$data);
			$this->load->view('jjr/block/footer');
		}
		
		
		if($state==2){
			$data['title']='提醒';
			$this->load->view('jjr/block/header',$header_data);
			$data['msg']="账号已经停用";
			$this->load->view('jjr/msg',$data);
			$this->load->view('jjr/block/_footer');		//底部菜单屏蔽掉
		}
		
		
	}
	
	/* index入口的导航逻辑，
	 * 不同的按钮将初始化flag表单不同的值，根据flag的值来决定导航到哪个页面
	 * 
	 * flag:决定了导航菜单的选择（参见jjr/index.php中tijiao(flag)）
	 * -1:注册 2:推荐 3:首页 4:信息 5:客户
	 * 
	 */
	function entrance_do(){
		$this->load->helper('url');
		$this->config->load('_myconfig.php');
		
		/* 导航菜单需要用到的 */
		$openId = isset($_POST['openId'])?$_POST['openId']:'';
		$state = isset($_POST['state'])?$_POST['state']:'';
		$item = isset($_POST['item'])?$_POST['item']:'';
		$uid = isset($_POST['uid'])?$_POST['uid']:'';
		$name = isset($_POST['name'])?$_POST['name']:'';
		$flag = isset($_POST['flag'])?$_POST['flag']:-1;
		
		
		$header_data['openId']=$openId;
		$header_data['state']=$state;
		$header_data['item']=$item;
		$header_data['uid']=$uid;
		$header_data['title']=$this->config->item('mytitle');;
		$header_data['showdebug']=$this->config->item('showdebug');
		
		
		
		
		if($state==2 || $state==0)
			$flag=999;
		
		$this->load->model('user_model');
		$user = $this->user_model->getUserByOpenId($openId);	//由openId查询user
		$uid = isset($user['id'])?$user['id']:0;

		
		$data['creator']=$uid;
		$data['uid']=$uid;
		$data['flag']=$flag;
		$data['openId']=$openId;
		$data['state']=$state;
		$data['flag']=$flag;
		
// 		echo $state;
		
		if($flag==1){	// 注册
			$data['title']='经纪人注册';
			$this->load->view('jjr/block/header',$header_data);
			$this->load->view('jjr/regist1',$data);
			$this->load->view('jjr/block/footer');
		}
		
		if($flag==2){	// 推荐客户
			$data['title']='推荐客户';
			$this->load->view('jjr/block/header',$header_data);
			$this->load->view('jjr/client1',$data);
			$this->load->view('jjr/block/footer');
		}
		
		if($flag==3){	// 首页
			$data['title']='首页';
			$this->load->view('jjr/block/header',$header_data);
			$this->load->view('jjr/index',$data);
			$this->load->view('jjr/block/footer');
		}
		
		if ($flag==999){
		    $data['title']='提醒';
			$this->load->view('jjr/block/header',$header_data);
			$data['msg']="请注册并等待审核通过";
			$this->load->view('jjr/msg',$data);
			$this->load->view('jjr/block/footer2');
		}
	}
	
	
	
	
}